<?php

namespace App\Models\miniapp;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    use HasFactory;
    public $table = 'miniapp_users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'openid',
        'unionid',
        'nickname',
        'avatar',
        'phone',
    ];

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return ['role' => 'miniapp'];
    }

    /**
     * 获取用户最近的登录信息。
     */
    public function closest()
    {
        return $this->hasOne(UserLogin::class, 'user_id', 'id')->latestOfMany('id');
    }

    /**
     * 获取用户的登录信息。
     */
    public function login()
    {
        return $this->hasMany(UserLogin::class, 'user_id', 'id');
    }

    /**
     * 获取用户的地址信息。
     */
    public function address()
    {
        return $this->hasMany(Address::class, 'user_id', 'id');
    }
}
